package com.dao.impl;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

/**
 * 数据库工具类
 * 
 * @author Administrator
 *
 */
public class DBHelper {

	private static String driver = null;
	private static String url = null;
	private static String username = null;
	private static String pwd = null;

	static {
		InputStream in = null;

		try {

			// 加载配置文件
			Properties ps = new Properties();
			in = DBHelper.class.getClassLoader().getResourceAsStream("db.properties");
			ps.load(in);

			// 获得驱动
			driver = ps.getProperty("driver");
			url = ps.getProperty("url");
			username = ps.getProperty("username");
			pwd = ps.getProperty("pwd");

			// 加载驱动
			Class.forName(driver);

		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * 获得连接
	 * @return
	 */
	public static synchronized Connection getConnection() {
		try {
			return DriverManager.getConnection(url, username, pwd);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	

}
